import {fetch} from '../../../utils/axios'
const state = {
  hostsNum: {},
  middleWareStatus: []
}

const mutations = {
  loadHostsNum (state) {
    fetch('/dashboard/count_for_hosts/', {
    }).then((response) => {
      state.hostsNum = JSON.parse(response)
    }).catch((error) => {
      console.log(error.response.data.detail)
    })
  },
  loadMiddleStatus (state) {
    fetch('/dashboard/middle_ware_status/', {
    }).then((response) => {
      let res = JSON.parse(response)
      state.middleWareStatus = []
      for (var i in res) {
        if (res[i]) {
          state.middleWareStatus.splice(0, 0, {avatar: 'arrow_upward', title: i, color: 'lime'})
        } else {
          state.middleWareStatus.splice(0, 0, {avatar: 'arrow_downward', title: i, color: 'error'})
        }
      }
    }).catch((error) => {
      console.log(error.response.data.detail)
    })
  }
}

const actions = {
  loadHostsNum ({commit}) {
    commit('loadHostsNum')
  },
  loadMiddleStatus ({commit}) {
    commit('loadMiddleStatus')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
